package tt.dz.order.service.rpc.rpchander;

import org.apache.avro.generic.GenericRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;

import tt.dz.order.exception.BusinessException;
import tt.dz.order.pojo.OrderInfo;
import tt.dz.order.service.OrderService;
import tt.dz.order.util.OrderConstant.BizSystemCode;
import tt.dz.order.vo.RetVO;

@Component("modifyOrderStatusHander")
public class ModifyOrderStatusHander implements BaseHander {
	private static Logger log = LoggerFactory.getLogger(ModifyOrderStatusHander.class);
	@Autowired
	OrderService orderService;

	@Override
	public RetVO dohander(GenericRecord req) {
		RetVO retVO = new RetVO(RetVO.SuccessCode, null, null);
		try {
			String reqStr = req.get("orderInfo").toString();
			log.info("更改订单状态reqStr：" + reqStr);
			OrderInfo sourceOrder = JSON.parseObject(reqStr, OrderInfo.class);
			orderService.modifyOrderStatus(sourceOrder);
		} catch (JSONException e) {
			log.error("更改订单状态发生异常", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setResultCode(new StringBuilder(BizSystemCode.OrderServer.value())
					.append(new BusinessException("").getExceptionCode()).append("00").toString());
			retVO.setErrorMsg(e.getMessage());
		} catch (BusinessException e) {
			log.error("更改订单状态发生异常", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setResultCode(e.getExceptionCode());
			retVO.setErrorMsg(e.getMessage());
		} catch (Exception e) {
			log.error("更改订单状态发生异常", e);
			retVO.setRetCode(RetVO.FailCode);
			retVO.setResultCode("1020300");
			retVO.setErrorMsg(e.getMessage());
		}
		return retVO;
	}

}
